package util;

import pojo.Contacts;

import java.nio.Buffer;
import java.nio.CharBuffer;

/**
 * 基本操作工具类
 * @author mayanwei
 */
public class Operation {
    /**
     * 格式化通讯簿各定长字段
     * @param contact
     */
    public static void rightFillBlank(Contacts contact){
        if(contact.getEmail()!= null){
            int length = contact.getEmail().length();
            for (int i = -1; i<(Contacts.emailLength -length ); i++){
                if(i !=-1){
                    contact.setEmail(contact.getEmail()+' ');
                }
            }
        }
        if(contact.getName()!= null){
            int length = contact.getName().length();
            for (int i = -1; i<(Contacts.nameLength - length); i++){
                if(i !=-1){
                    contact.setName(contact.getName()+' ');
                }
            }
        }
        if(contact.getPhoneNumber()!= null){
            int length = contact.getPhoneNumber().length();
            for (int i = -1; i<(Contacts.phoneNumberLength - length); i++){
                if(i != -1){
                    contact.setPhoneNumber(contact.getPhoneNumber()+' ');
                }
            }
        }

    }

    /**
     * 将整数 num 转为长度为 strlen 的字符串。位数不够前面补 0
     * <p>
     * 要求strlen要大于数字的位数。
     * eg: 12 --> 012
     * @param num
     * @param strlen
     * @return
     */
    public static String getFormatedStringFromInteger(Integer num , int strlen){
        StringBuffer strBuffer = null;
        if (num!=null && num.toString().length()<=strlen){
            strBuffer = new StringBuffer(strlen);
            for (int i = -1;i<(strlen - String.valueOf(num).length());i++){
                if(i!= -1)strBuffer.append("0");
            }
            strBuffer.append(num.toString());
        }else {
            throw new RuntimeException("越界错误，num的长度应不大于strlen");
        }
        return strBuffer != null?strBuffer.toString():null;
    }

    /**
     * 例如：
     * byte b = 49
     * Long.toString(b[i] & 0xff, 2)  ==>  "110001"
     *
     * 这里返回长度为定长的8位 "00110001"
     * @param b
     * @return
     */
    public static String getFormatedBinaryStringFromBytes(byte [] b){
        StringBuffer result = null;
        if(b!=null){
           result = new StringBuffer();
            StringBuffer btemp = new StringBuffer();
           for (int i = 0;i<b.length;i++ ){
               String s = Long.toString(b[i] & 0xff, 2);
               btemp.append(s);
               if (btemp.length() < 8){
                   int lenTmp = btemp.length();
                   for (int j=0; j< 8 - lenTmp;j++)
                       btemp.insert(0,'0');
               }
               result.append(btemp.toString());
               result.append(" ");
               btemp.delete(0,btemp.length());
           }
       }
       return result != null ? result.toString():null;
    }
    /**
     * 字节数组替换
     * @param originalBytes  需要替换原数组
     * @param offset    原数组偏移位置
     * @param len		原数组替换的长度 （替换原数组旧内容offset 到len）
     * @param replaceBytes   目标数组（替换新的内容）
     * @return
     */
    public static byte[] bytesReplace(byte[] originalBytes, int offset, int len, byte[] replaceBytes) {
        byte[] newBytes = new byte[originalBytes.length + (replaceBytes.length - len)];
        System.arraycopy(originalBytes, 0, newBytes, 0, offset);
        System.arraycopy(replaceBytes, 0, newBytes, offset, replaceBytes.length);
        System.arraycopy(originalBytes, offset + len, newBytes, offset+replaceBytes.length, originalBytes.length - offset -len);
        return newBytes;

    }
    /**
     * byte数组转换为二进制字符串,每个字节以" "隔开
     **/
    public static String byteArrToBinStr(byte[] b) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String s = Integer.toBinaryString(b[i]);
            result.append(s);
            result.append(" ");
        }
        return result.toString().substring(0, result.length() - 1);
    }

    /**
     * 二进制字符串转换为byte数组,每个字节以" "隔开
     **/
    public static byte[] binStrToByteArr(String binStr) {
        String[] temp = binStr.split(" ");
        byte[] b = new byte[temp.length];
        for (int i = 0; i < b.length; i++) {
            b[i]= Byte.parseByte(temp[i], 2);
        }
        return b;
    }

}
